package com.xpec.c4.service.message;

import com.xpec.c4.codec.CodecUtility;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.service.group.datalayer.IShareGroupData;
import com.xpec.c4.service.group.transportlayer.GroupMessage;

/**
 * 游戏的封装消息
 * 
 * @author Bany
 * 
 * @since 2008-8-30 下午08:14:35
 * 
 */

public class NetMessage implements Cloneable,IShareGroupData {

	/**
	 * @author suyinglong
	 * @version 2012-9-12 下午04:28:01
	 */
	private static final long serialVersionUID = 3835159278961549348L;

	/**
	 * 消息类型
	 */
	private int messageType;

	/**
	 * 消息内容
	 */
	private Object messageContent;
	
	/**
	 * 消息内容编码后的内容
	 */
	private byte[] messageContentBytes;

	/**
	 * 是否压缩
	 */
	private boolean compress = false;

	/**
	 * 消息的优先级
	 */
	private int priority = 0;

	/**
	 * 消息处理的sessionID
	 */
	private GUID sessionID;

	/**
	 * 消息处理的模块ID
	 */
	private String moduleID;
	
	public NetMessage() 
	{
	}
	
	public NetMessage(GroupMessage msg) 
	{
		this.messageType = msg.getMessageType();
		if( msg.isToClient() )
			this.messageContentBytes = msg.getMessageContentBytes();
		else
		{
			Object content = CodecUtility.decodeByte( msg.getMessageContentBytes() );
			setMessageContent(content);
		}
	}
	
	public NetMessage(int messageType, byte[] messageContentBytes, GUID sessionID) 
	{
		this.messageType = messageType;
		this.sessionID = sessionID;
		
		this.messageContent = null;
		if( messageContentBytes == null )
		{
			this.messageContentBytes = CodecUtility.contentEncode(this.messageContent,messageType);
		}
		else
			this.messageContentBytes = messageContentBytes;
	}

	public NetMessage(int messageType, Object messageContent, GUID sessionID) 
	{
		this.messageType = messageType;
		this.sessionID = sessionID;
		
		setMessageContent(messageContent);
	}
	
	public NetMessage(int messageType, Object messageContent) 
	{
		this.messageType = messageType;
		setMessageContent(messageContent);
	}
	
	public int getMessageType() {
		return messageType;
	}

	public void setMessageType(int messageType) {
		this.messageType = messageType;
	}

	public Object getMessageContent() 
	{
		return messageContent;
	}

	public void setMessageContent(Object messageContent) 
	{
		this.messageContent = messageContent;
		this.messageContentBytes = CodecUtility.contentEncode(messageContent,messageType);
	}

	public void checkCreateMessageContentBytes() 
	{
		if( this.messageContent != null )
		{
			this.messageContentBytes = CodecUtility.contentEncode(this.messageContent,messageType);
		}
		else
		{
			this.messageContentBytes = null;
		}
	}
	
	public boolean isCompress() {
		return compress;
	}

	public void setCompress(boolean compress) {
		this.compress = compress;
	}

	public int getPriority() {
		return priority;
	}

	public void setPriority(int priority) {
		this.priority = priority;
	}

	public GUID getSessionID() {
		return sessionID;
	}

	public void setSessionID(GUID sessionID) {
		this.sessionID = sessionID;
	}

	@Override
	public NetMessage clone() {
		NetMessage obj = null;
		try {
			obj = (NetMessage) super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return obj;
	}

	public String getModuleID() {
		return moduleID;
	}

	public void setModuleID(String moduleID) {
		this.moduleID = moduleID;
	}
	
	public byte[] getMessageContentBytes() {
		return messageContentBytes;
	}

	public void setMessageContentBytes(byte[] messageContentBytes) {
		this.messageContentBytes = messageContentBytes;
	}
	
	/**
	 * set messageContent null, but not messageContentBytes
	 *
	 * @author suyinglong
	 * @version 2012-9-12 下午04:31:03
	 */
	public void clearMessageContent(){
		messageContent = null;
	}

	@Override
	public String toString() {
		return "type:" + getMessageType()
			//+ "\tobject:" + getMessageContent()
			+ "\tobject:" + getMessageContentBytes()
			+ "\tmoduleID:" + getModuleID()
			+ "\tsessionID:" + getSessionID();
	}
}
